package com.dogstudio.popstart.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;

import com.dogstudio.popstart.utils.AppUtils;
import com.dogstudio.popstart.utils.LruLoader;


public class StartView extends View {
    private int type;
    private boolean isSelect = false;
    private Point mPoint;
    private int mSize;
    private Bitmap normalBitmap;
    private Bitmap selBgBitmap;
    private Paint mPaint;
    private Matrix matrixNor;
    private Matrix matrixSel;

    public StartView(Context context) {
        super(context);
        init();
    }

    public StartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        type = AppUtils.getRandom();
        normalBitmap = LruLoader.get().getBitmapByType(type);
        selBgBitmap = LruLoader.get().getSelBitmap();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mSize = AppUtils.getStartSize();
        matrixNor = AppUtils.getScaleMatrix(normalBitmap.getWidth(), mSize);
        matrixSel = AppUtils.getScaleMatrix(selBgBitmap.getWidth(), mSize);
    }

    public void setSelectState(boolean b) {
        isSelect = b;
        invalidate();
    }

    public int getType() {
        return type;
    }

    public void setPosition(Point p) {
        mPoint = p;
    }

    public Point getPosition() {
        return mPoint;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mSize, mSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(normalBitmap, matrixNor, mPaint);
        if (isSelect) {
            canvas.drawBitmap(selBgBitmap, matrixSel, mPaint);
        }
    }


    @Override
    public String toString() {
        return "StartView{" +
                "type=" + type +
                ", isSelect=" + isSelect +
                ", mPoint=" + mPoint +
                '}';
    }
}
